﻿package app.view.components.video 
{
	import app.view.components.buttons.SelectableButton;
	import fl.video.FLVPlayback;
	import fl.video.VideoEvent;
	import fl.video.VideoPlayer;
	import fl.video.VideoProgressEvent;
	import fl.video.VideoState;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.media.Video;
	import org.casalib.display.CasaSprite;
	
	/**
	 * Видео бокс, распологается в попапе POPUP_CHILDREN_WINER_INTERVIEW (// 2.1.2.1 )
	 * @author firsoff maxim, snzflash.com, tel: +79222345364, skype: maxim.firsoff
	 */
	public class VideoBoxChildrenBase extends CasaSprite
	{
		public var  videoContainer:VideoContainer;
		public var  progressBar:ProgressBar;
		public var  playBtn:SelectableButton;
		private var _video:FLVPlayback;
		private var _source:String;
		private var _state:String = 'play';
		public var autoPlay:Boolean = false;
		
		public function VideoBoxChildrenBase() 
		{
			super();
			init();
		};
		
		private function init():void
		{
			_video = videoContainer.player;
			_video.autoPlay = autoPlay;
			_video.bufferTime = 1.5;
			//_video.muteButton = new Sprite();
			VideoPlayer(_video.getVideoPlayer(0)).smoothing = true;
			playBtn.autoEnable = false;
			playBtn.addEventListener(MouseEvent.CLICK, onClickPlayBtn);
			
			progressBar.addEventListener(Event.CHANGE, onProgressbarChanged);
			//trace(_video.getVideoPlayer[0]);
			//VideoPlayer(_video.getVideoPlayer[0]).smoothing = true;
			_video.addEventListener(VideoProgressEvent.PROGRESS, onVideoLoading);
			_video.addEventListener(VideoEvent.PLAYHEAD_UPDATE, onVideoPlay);
			_video.addEventListener(VideoEvent.COMPLETE, onVideoComplete);
		};
		
		private function onVideoComplete(event:VideoEvent):void 
		{
			dispatchEvent(event.clone());
		};
		
		private function onProgressbarChanged(event:Event):void 
		{
			//trace(progressBar.seekValue);
			_video.seek( progressBar.seekValue * _video.totalTime);
		};
		
		private function onClickPlayBtn(event:MouseEvent):void 
		{
			if ( _state == 'play') 
			{
				_video.pause();
				playBtn.selected = false;
				_state = 'pause';
			}
			else 
			{
				_video.play();
				playBtn.selected = true;
				 _state = 'play'
			};
		};
		
		/**
		 * сброс видео бокса
		 */
		public function reset():void 
		{
			videoContainer.player.stop();
			
			var vp:VideoPlayer = _video.getVideoPlayer(0);
			
			
			try 
			{
				vp.clear();
				vp.close();
			}
			catch (err:Error)
			{
				
			}
			
			visible = false;
			
			//if (vp) vp.stop();
			progressBar.reset();
			
			progressBar.visible = false;
			playBtn.visible = false;
			
		};
		
		override public function destroy():void 
		{
			super.destroy();
			_video.removeEventListener(VideoProgressEvent.PROGRESS, onVideoLoading);
			_video.removeEventListener(VideoEvent.PLAYHEAD_UPDATE, onVideoPlay);
			_video.removeEventListener(VideoEvent.COMPLETE, onVideoComplete);
			_video.stop();
			try {_video.closeVideoPlayer(0);}
			catch (err:Error) { }
			_video = null;
			
			progressBar.removeEventListener(Event.CHANGE, onProgressbarChanged);
			progressBar.destroy();
			progressBar = null;
			
			playBtn.removeEventListener(MouseEvent.CLICK, onClickPlayBtn);
			
		};
		
		public function pause():void
		{
			_video.pause();
			playBtn.selected = false;
			_state = 'pause';
		}
		
		private function onVideoPlay(event:VideoEvent):void 
		{
			//trace(event.playheadTime / _video.totalTime);
			progressBar.progressPlaying = event.playheadTime / _video.totalTime;
			//trace('progressBar.progressPlaying : ' + progressBar.progressPlaying);
		};
		
		private function onVideoLoading(event:VideoProgressEvent):void 
		{
			progressBar.progressLoading = event.bytesLoaded / event.bytesTotal;
			//trace('progressBar.progressLoading : ' + progressBar.progressLoading);
		};
		
		public function get source():String { return _source; };
		
		public function set source(value:String):void 
		{
			_source = value;
			
			reset();
			
			
			if ( value == '' || !value) 
			{
				progressBar.visible = false;
				playBtn.visible = false;
				return;
			}
			else 
			{
				progressBar.visible = true;
				playBtn.visible = true;
			};
			
			trace('****',_source);
			visible = true;
			_video.source = value;
			
			//_video.play();
			//playBtn.selected = true;
		};
	};
}; 	